package com.autonavi.yunda.yunji.configuration;

import com.autonavi.yunda.yunji.common.web.AmapTraceIdFilter;
import com.autonavi.yunda.yunji.common.web.RequestResponseLoggingFilter;
import com.autonavi.yunda.yunji.common.web.YunjiCorsFilter;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import java.time.format.DateTimeFormatter;

/**
 * @author cizhong.wcs
 * @date 2022/2/10 3:58 下午
 */
@Configuration
public class WebConfiguration {

    private static final String DATE_FORMAT = "yyyy-MM-dd";
    private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    @Bean(name = "multipartResolver")
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setDefaultEncoding("UTF-8");
        //resolveLazily属性启用是为了推迟文件解析，在UploadAction中捕获文件大小异常
        resolver.setResolveLazily(true);
        resolver.setMaxInMemorySize(40960);
        //上传文件大小 50M 50*1024*1024
        resolver.setMaxUploadSize(50 * 1024 * 1024);
        return resolver;
    }

    @Bean
    public FilterRegistrationBean<AmapTraceIdFilter> traceIdFilterBean() {
        FilterRegistrationBean<AmapTraceIdFilter> bean = new FilterRegistrationBean<>();
        AmapTraceIdFilter filter = new AmapTraceIdFilter();
        bean.setFilter(filter);
        return bean;
    }

    @Bean
    public FilterRegistrationBean<RequestResponseLoggingFilter> requestResponseLoggingFilterBean() {
        FilterRegistrationBean<RequestResponseLoggingFilter> bean = new FilterRegistrationBean<>();
        RequestResponseLoggingFilter filter = new RequestResponseLoggingFilter();
        bean.setFilter(filter);
        return bean;
    }

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
        return builder -> {
            builder.simpleDateFormat(DATE_TIME_FORMAT);
            builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_FORMAT)));
            builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)));
        };
    }

    @Bean
    public MappingJackson2XmlHttpMessageConverter xmlHttpMessageConverter() {
        return null;
    }


    @Bean
    public FilterRegistrationBean yunjiCorsFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new YunjiCorsFilter());
        registration.addUrlPatterns("/*");
        registration.setName("yunjiCorsFilter");
        return registration;
    }


}
